package com.witsight.hibernate.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.witsight.platform.model.BaseObject;
import com.witsight.platform.util.lang.StringUtil;
/**   
 * 说明：数据库实体继承此抽象对象
 * @Title: BaseEntity.java 
 * @Package com.witsight.hibernate.model 
 * @See: {@link BaseObject}
 * Copyright: Copyright (c) 2017
 * Company:sany huax witsight team by product
 * @author: fuyp  
 * @date: 2017年9月12日 上午11:39:39 
 * @version: V1.0
 *
 */
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity extends BaseObject<String>{
	private static final long serialVersionUID = -7561609322014032054L;
	
	// 主键
	private String id;
	// 最后修改时间
	private Date lastModify;
	// 版本号
	private Long version;
	// 创建时间
	private Date createDate;
	
	@Id
	@GeneratedValue( generator = "uuid" )
	@GenericGenerator( name = "uuid", strategy = "uuid2" )
	@Column(length=50)
	@Override
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Version
	@Column(name = "VERSION")
	public Long getVersion() {
		return version;
	}

	public void setVersion(Long version) {
		this.version = version;
	}
	
	@CreatedDate
	@Column(name = "CREATE_DATE")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	@LastModifiedDate
	@Column(name = "LAST_MODIFY")
	public Date getLastModify() {
		return lastModify;
	}

	public void setLastModify(Date lastModify) {
		this.lastModify = lastModify;
	}
	
	@Transient
	@Override
	public boolean isNew() {		// true 表示保存 false 表示更新
		return StringUtil.isEmpty(this.getId());
	}
	
	@Override
	protected ToStringBuilder toStringBuilder() {
		return super.toStringBuilder()
				.append("version",version)
				.append("lastModify",lastModify)
				.append("createDate",createDate);
	}
}
